﻿(function() {
    $(function() {

        // 颜色16进制验证   
        $.validator.addMethod("isColorStyle", function (value, element) {
            var tel = /^#[0-9,a-f,A-F]{6}$/;
            return this.optional(element) || (tel.test(value));
        }, "请正确填写颜色样式");

        var _projectService = abp.services.app.project;
        var _$modal = $('#EnvironmentCreateModal');
        var _$form = _$modal.find('form');

        _$form.validate({
            rules: {
                Order: {
                    required: true,
                    digits:true
                },
                Color: {
                    isColorStyle: true
                }
            }
        });


        $('#RefreshButton').click(function () {
            refreshUserList();
        });

        $('.delete-environment').click(function () {
            var environmentId = $(this).attr("data-environment-id");
            var environmentName = $(this).attr('data-environment-name');
            deleteEnvironment(environmentId, environmentName);
        });

        $('.edit-environment').click(function (e) {
            var environmentId = $(this).attr("data-environment-id");

            e.preventDefault();
            $.ajax({
                url: abp.appPath + 'Projects/EditEnvironmentModal?environmentId=' + environmentId,
                type: 'POST',
                contentType: 'application/html',
                success: function (content) {
                    $('#EnvironmentEditModal div.modal-content').html(content);
                },
                error: function (e) { }
            });
        });

        _$form.find('button[type="submit"]').click(function (e) {
            e.preventDefault();
            if (!_$form.valid()) {
                return;
            }

            var environment = _$form.serializeFormToObject(); //serializeFormToObject is defined in main.js

            abp.ui.setBusy(_$modal);
            _projectService.createEnvironment(environment).done(function () {
                _$modal.modal('hide');
                location.reload(true); //reload page to see new user!
            }).always(function () {
                abp.ui.clearBusy(_$modal);
            });
        });

        _$modal.on('shown.bs.modal', function () {
            _$modal.find('input:not([type=hidden]):first').focus();
        });

        //初始化分页
        $.paginationInit($('.pagination'), '', 7);

        function refreshUserList() {
            location.reload(true); //reload page to see new user!
        }

        function deleteEnvironment(environmentId, environmentName) {
            abp.message.confirm(
                abp.utils.formatString(abp.localization.localize('AreYouSureWantToDelete', 'DevM'), environmentName),
                function (isConfirmed) {
                    if (isConfirmed) {
                        _projectService.deleteEnvironment({
                            id: environmentId
                        }).done(function () {
                            refreshUserList();
                        });
                    }
                }
            );
        }
    });
})();
